#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\math\sampledcurve.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Math {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ISampledCurve
	public ref class CSampledCurve : 
            public System::MarshalByRefObject,
            public Cephei::QL::Math::ISampledCurve
	{
	protected: 
		boost::shared_ptr<QuantLib::SampledCurve>* _ppSampledCurve;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::SampledCurve>* _phSampledCurve;
#endif
		Object^ _SampledCurveOwner;     // reference to object that manages the storage for this object
	internal:
		CSampledCurve (Cephei::QL::Math::IArray^ grid);
		CSampledCurve (Microsoft::FSharp::Core::FSharpOption<UInt64>^ gridSize);
        CSampledCurve (boost::shared_ptr<QuantLib::SampledCurve>& childNative, Object^ owner);
        CSampledCurve (QuantLib::SampledCurve& childNative, Object^ owner);
        CSampledCurve (CSampledCurve^ copy);
        CSampledCurve (System::Type^ t);
#ifdef STRUCT
        CSampledCurve (QuantLib::SampledCurve childNative);
#endif       
#ifdef HANDLE
		CSampledCurve (QuantLib::Handle<QuantLib::SampledCurve>& childNative, Object^ owner);
		CSampledCurve (QuantLib::Handle<QuantLib::SampledCurve> childNative);
#endif
		virtual ~CSampledCurve ();
		!CSampledCurve ();

	internal:
		QuantLib::SampledCurve& GetReference ();
		boost::shared_ptr<QuantLib::SampledCurve>& GetShared ();
		QuantLib::SampledCurve* GetPointer ();
        void SetSampledCurve (boost::shared_ptr<QuantLib::SampledCurve> native)
        {
            if (_ppSampledCurve != NULL)
                delete _ppSampledCurve;
            _ppSampledCurve = new boost::shared_ptr<QuantLib::SampledCurve> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::SampledCurve>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property Double FirstDerivativeAtCenter 
        {
		    virtual Double get () ;
        }
        property Cephei::QL::Math::IArray^ Grid 
        {
		    virtual Cephei::QL::Math::IArray^ get () ;
        }
		virtual Double GridValue (UInt64 i) ;
		virtual Cephei::QL::Math::ISampledCurve^ Regrid (Cephei::QL::Math::IArray^ new_grid) ;
		virtual Cephei::QL::Math::ISampledCurve^ RegridLogGrid (Double min, Double max) ;
		virtual Cephei::QL::Math::ISampledCurve^ ScaleGrid (Double s) ;
        property Double SecondDerivativeAtCenter 
        {
		    virtual Double get () ;
        }
		virtual Cephei::QL::Math::ISampledCurve^ SetGrid (Cephei::QL::Math::IArray^ prm1) ;
		virtual Cephei::QL::Math::ISampledCurve^ SetLogGrid (Double min, Double max) ;
		virtual Cephei::QL::Math::ISampledCurve^ SetValues (Cephei::QL::Math::IArray^ prm1) ;
		virtual Cephei::QL::Math::ISampledCurve^ ShiftGrid (Double s) ;
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
		virtual Cephei::QL::Math::ISampledCurve^ Swap (Cephei::QL::Math::ISampledCurve^ prm1) ;
		virtual Double Value (UInt64 i) ;
        property Double ValueAtCenter 
        {
		    virtual Double get () ;
        }
        property Cephei::QL::Math::IArray^ Values 
        {
		    virtual Cephei::QL::Math::IArray^ get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CSampledCurve_Factory : public System::MarshalByRefObject,  public ISampledCurve_Factory
	{
	public:
        virtual ISampledCurve^ Create (Cephei::QL::Math::IArray^ grid);
        virtual ISampledCurve^ Create (Microsoft::FSharp::Core::FSharpOption<UInt64>^ gridSize);
    };
   
/*Cephei*/ } /*QL*/ } /*Math */}
